<template>
  <div>
    <el-button type="success" @click="handleAddClick" size="small">添加售前项目</el-button>
    <el-input style="width: 220px" v-model="input" placeholder="请输入内容" size="small"></el-input>
    <el-button type="primary" @click="handleSearchClick" size="small">搜索</el-button>
    <el-button type="danger" v-show="activeName === 'first'" @click="del" size="small">删除</el-button>
    <div class="main">
      <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
        <el-tab-pane label="售前项目" name="first">
          <transition name="el-fade-in">
            <Table v-show="flag" ref="first" :setting="firstSetting" @handleCommonClick="handleCommonClick"
              @selectedValue="selectChange" @handleSortTable="handleSortTable" />
          </transition>
        </el-tab-pane>
        <el-tab-pane label="转正记录" name="second" lazy>
          <transition name="el-fade-in">
            <Table v-show="!flag" ref="second" :setting="secondSetting" @handleCommonClick="handleCommonClick" />
          </transition>
        </el-tab-pane>
      </el-tabs>
      <regularizationDialog ref="regularizationDialog" @dialogResults="dialogResults" />
      <Dialog ref="dialog" @dialogResult="dialogResult" />
      <recordDialog ref="recordDialog" />
    </div>
  </div>
</template>

<script>
import { project, minutes } from "./component/setting";
import {
  beforeSaleProjectAdd,
  updateBeforeSalePrj,
  setPrjFormal,
  deleteBeforeSale,
  beforeSaleProjectList,
} from "@/service/Api";
import Table from "./component/table";
import Dialog from "./component/dialog";
import recordDialog from "./component/recordDialog";
import regularizationDialog from "./component/regularizationDialog"

export default {
  data() {
    return {
      input: "",
      flag: true,
      activeName: "first",
      firstSetting: project,
      secondSetting: minutes,
      options: [],
      value: "",
      delData: [],
      sortColunm: undefined,
      sortWay: undefined,
    };
  },
  components: {
    Table,
    Dialog,
    recordDialog,
    regularizationDialog
  },
  // watch:{
  //   regularizationDialog (){
  //     this.beforeSaleProjectList();
  //   }
  // },
  methods: {
    handleClick(tab, event) {
      console.log("打印一下", tab.name);
      this.activeName = tab.name;
      if (this.activeName === "first") {
        this.flag = true;
        this.$refs.first.refresh();
      } else {
        this.flag = false;
        this.$nextTick(() => {
          this.$refs.second.refresh();
        });
      }
      // this.flag = !this.flag;
      // this.flag ? this.$refs.first.refresh() : this.$refs.second.refresh();
    },
    // 转为正式项目
    tableBecome(row) {
      this.$refs.regularizationDialog.showDialog(row)
      // setPrjFormal(row.id).then((response) => {
      //   if (response.code === 0) {
      //     this.$message.success("操作成功");
      //     this.$refs.first.refresh();
      //   }
      // });
    },
    dialogResults(results) {
      setPrjFormal(results).then((response) => {
        if (response.code === 0) {
          this.$message.success("操作成功");
          this.$refs.regularizationDialog.close();
          this.$refs.first.refresh();
        }
      });
    },
    // 查看日报
    handleDaily(row) {
      console.log("售前项目查看项目", row);
      // /allDaily
      this.$router.push({
        path: "/allDaily",
        query: { projectId: row.id, projectName: row.name, condition: 0 },
      });
    },
    //搜索
    handleSearchClick() {
      if (this.activeName == "first") {
        this.$refs.first.refresh({
          projectKeyWord: this.input,
          isPreSale: "0",
          sortColunm: this.sortColunm,
          sortWay: this.sortWay,
        });
      } else {
        this.$refs.second.refresh({
          projectKeyWord: this.input,
          isPreSale: "1",
        });
      }
    },
    //添加
    handleAddClick() {
      this.$refs.dialog.showDialog();
    },
    handleCommonClick(row, name) {
      this[name](row);
    },
    tableEdit(row) {
      this.$refs.dialog.showDialog(row);
    },
    tabStu(row) {
      console.log(row);

      this.$emit("subset", { falg: 1, projectid: row.id, name: row.name });
    },
    tableHours(row) {
      this.$emit("subset", { falg: 2, projectid: row.id });
    },
    tableRecord(row) {
      //请求数据
      this.$store.dispatch("getProjectList", { id: row.id, pn: 1, ps: 5 });
      this.$refs.recordDialog.showDialog(row, this.activeName);
    },
    selectChange(val) {
      this.delData = val.map((item) => item.id);
    },
    // 排序
    handleSortTable(val) {
      let str = "";
      switch (val.val) {
        case "intention":
          str = "intention";
          break;
        case "saleStatus":
          str = "sale_status";
          break;
        case "createTime":
          str = "create_time";
          break;
        // case "planEndDate":
        //   str = "plan_end_date";
        //   break;
        // case "devStatus":
        //   str = "dev_status";
        //   break;
        default:
          str = "";
      }
      this.sortColunm = str;
      this.sortWay = val.flag;
      this.handleSearchClick();
    },

    del() {
      if (this.delData.length === 0) {
        this.$message.info("请先选择数据");
        return;
      }
      this.$confirm("此操作将永久删除该项目, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteBeforeSale(this.delData).then((res) => {
            if (res.code === 0) {
              this.$message.success("删除成功");
              this.$refs.first.refresh();
              this.delData = [];
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    dialogResult(result, title) {
      if (title === "添加") {
        beforeSaleProjectAdd(result).then((response) => {
          if (response.code === 0) {
            this.$message.success("添加成功");
            this.$refs.dialog.close();
            this.$refs.first.refresh();
          }
        });
      } else {
        // 编辑时无需求责任人，删除demandUserId参数
        let { demandUserId, ...data } = result;
        updateBeforeSalePrj(data).then((response) => {
          if (response.code === 0) {
            this.$message.success("修改成功");
            this.$refs.first.refresh();
          }
        });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.main {
  margin-top: 20px;
  text-align: right;

  .danger {
    margin: -8px 0px 10px -72px;
    position: absolute;
  }
}
</style>
